package com.xiaomi.baidumap.github;

import android.content.Context;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.apache.commons.io.IOUtil;

import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.List;

/**
 * Created by Administrator on 16-8-30.
 */
public class Language implements Serializable{

    private String path;
    private String name;
    private static List<Language> languages;

    //对本地json解析出来
    public static List<Language> getDefaultLanguage(Context context){
        if (languages != null){
            return languages;
        }

        try {
            InputStream inputStream = context.getAssets().open("langs.json");
            //io直接利用第三方来转换成字符串
            String content = IOUtil.toString(inputStream);
            //然后gson解析，把字符串解析成一个集合
            Gson gson = new Gson();
            languages = gson.fromJson(content, new TypeToken<List<Language>>() {
            }.getType());
            return languages;
        } catch (IOException e) {
            //可以直接抛出异常
           throw new RuntimeException();
        }

    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Language{" +
                "path='" + path + '\'' +
                ", name='" + name + '\'' +
                '}';
    }
}
